﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ImapX;
using System.Diagnostics;
using System.ComponentModel;
using ImapTest.Properties;
using ImapX.Parsing.Commands;
using ImapX.Parsing.DataItems;
using System.Text.RegularExpressions;

namespace ImapTest
{
    public struct MessageLocation
    {
        public MessageLocation(string folderPath, int uid)
        {
            this.folderPath = folderPath;
            this.uid = uid;
        }

        public string folderPath;
        public int uid;
    }
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            if (Settings.Default.srcPassword != "")
            {
                srcPassword.Password = Password.Decrypt(Settings.Default.srcPassword);
            }
        }

        private ImapClient srcClient;


        private void btnRun_Click(object sender, RoutedEventArgs e)
        {
            srcClient = new ImapClient("imap.gmail.com", 993, true);
            srcClient.IsDebug = true;

            if (!srcClient.Connect())
                throw new Exception("Connection failed");

            srcClient.LogIn(srcEmailTextBox.Text, srcPassword.Password);
        }

        public ObservableCollection<string> StatusMessages = new ObservableCollection<string>();

        private void Error(string message)
        {
            UI(() =>
            {
                StatusMessages.Add(message);
            });
        }

        public void UI(Action action)
        {
            Dispatcher.Invoke(action);
        }

        private bool SavePasswords
        {
            get
            {
                return savePasswordsCheckBox.IsChecked.GetValueOrDefault(false);
            }
        }

        private void srcPassword_PasswordChanged(object sender, RoutedEventArgs e)
        {
            string password = "";
            if (SavePasswords)
            {
                password = Password.Encrypt(srcPassword.Password);
            }
            Settings.Default.srcPassword = password;
            Settings.Default.Save();
        }

        private void savePasswordsCheckBox_Click(object sender, RoutedEventArgs e)
        {
            string srcPwd = "";

            if (savePasswordsCheckBox.IsChecked.GetValueOrDefault(false))
            {
                srcPwd = Password.Encrypt(srcPassword.Password);
            }

            Settings.Default.srcPassword = srcPwd;

            Settings.Default.Save();
        }

        private void srcDebugCheckBox_Click(object sender, RoutedEventArgs e)
        {
            if (srcClient != null)
            {
                srcClient.IsDebug = srcDebugCheckBox.IsChecked.GetValueOrDefault(false);
            }
        }

        private void commandTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                string command = commandTextBox.Text + "\r\n";
                resultsTextbox.Text += command;
                srcClient._client.SendCommand(command);

                string result = srcClient._client.ReceiveResult(false);
                resultsTextbox.Text += result;

                resultsTextbox.ScrollToEnd();
                e.Handled = true;
            }
        }
    }
}
